/*
 * @Description: 
 * @Author: Jimmy
 * @Date: 2023-07-23 20:51:42
 * @LastEditTime: 2023-08-06 19:18:18
 * @LastEditors:  
 */
import { Op } from "sequelize"
import User from "../model/user.model"
import Depart from "../model/depart.model"
import Role from "../model/role.model"
import { Sequelize } from "sequelize";

interface IUser {
    id?: string
    username?: string
    nickname?: string
    phone?: string
    avatar?: string
    role_id?: number
    depart_id?: number
    status?: boolean
    password?: string
    is_admin?: number
}

class UseService {
    //创建用户
    async createUser({ username, password, is_admin, nickname, phone, avatar, role_id, depart_id }: IUser) {
        //写入数据库
        const res = await User.create({ username, password, is_admin, nickname, phone, avatar, role_id, depart_id });
        return res.dataValues
    }
    //获取用户信息
    async getUserInfo({ id, username, phone, nickname }: IUser): Promise<any> {
        if (id) {
            let options = { id }
            let res = await User.findOne({
                where: options,
                attributes: {
                    include: [
                        [Sequelize.col('depart_info.name'), 'dept_name'],
                        [Sequelize.col('role_info.name'), 'role_name'],
                    ]
                },
                include: [
                    {
                        model: Depart,
                        as: 'depart_info',//别名
                        attributes: [], //depart:{name:''}
                    },
                    {
                        model: Role,
                        as: 'role_info',//别名
                        attributes: [], //depart:{name:''}
                    }
                ]
            });
            if (res?.dataValues) {
                return res?.dataValues
            }
        }
        if (username) {
            let options = { username }
            let res = await User.findOne({
                where: options,
                attributes: {
                    include: [
                        [Sequelize.col('depart_info.name'), 'dept_name'],
                        [Sequelize.col('role_info.name'), 'role_name'],
                    ]
                },
                include: [
                    {
                        model: Depart,
                        as: 'depart_info',//别名
                        attributes: [], //depart:{name:''}
                    },
                    {
                        model: Role,
                        as: 'role_info',//别名
                        attributes: [], //depart:{name:''}
                    }
                ]
            });
            if (res?.dataValues) {
                return res.dataValues
            }
        }
        if (nickname) {
            let options = { nickname }
            let res = await User.findOne({
                where: options,
                attributes: {
                    include: [
                        [Sequelize.col('depart_info.name'), 'dept_name'],
                        [Sequelize.col('role_info.name'), 'role_name'],
                    ]
                },
                include: [
                    {
                        model: Depart,
                        as: 'depart_info',//别名
                        attributes: [], //depart:{name:''}
                    },
                    {
                        model: Role,
                        as: 'role_info',//别名
                        attributes: [], //depart:{name:''}
                    }
                ]
            });
            if (res?.dataValues) {
                return res.dataValues
            }
        }
        if (phone) {
            let options = { phone }
            let res = await User.findOne({
                attributes: ['id', 'username', 'nickname', 'phone', 'avatar', 'role_id', 'depart_id', 'status', 'password', 'is_admin'],
                where: options
            });
            if (res?.dataValues) {
                return res?.dataValues
            }
        }
    }
    //根据id更新用户信息
    async updateById({ id, nickname, phone, avatar, role_id, depart_id, status, is_admin }: IUser) {
        let whereOpt = { id };
        let newUser = { status, is_admin };
        // username && Object.assign(newUser, { username })
        nickname && Object.assign(newUser, { nickname })
        phone && Object.assign(newUser, { phone })
        avatar && Object.assign(newUser, { avatar })
        role_id && Object.assign(newUser, { role_id })
        depart_id && Object.assign(newUser, { depart_id })
        const res = await User.update(newUser, { where: whereOpt });
        return res[0] > 0 ? true : false
    }
    //根据id删除
    async deleteById(ids) {
        const res = await User.destroy({ where: { id:ids } })
        return res > 0 ? true : false;
    }
    //查询用户列表
    async getUserList({ username, nickname, phone, page, size, depart_id, role_id, is_admin, status }) {
        let whereOpt = {};
        username && Object.assign(whereOpt, { username: { [Op.like]: `%${username}%` } })
        nickname && Object.assign(whereOpt, { nickname: { [Op.like]: `%${nickname}%` } })
        phone && Object.assign(whereOpt, { phone })
        depart_id && Object.assign(whereOpt, { depart_id })
        role_id && Object.assign(whereOpt, { role_id })
        is_admin && Object.assign(whereOpt, { is_admin })
        status && Object.assign(whereOpt, { status })
        const res = await User.findAndCountAll({
            where: whereOpt,
            offset: (page - 1) * size,
            limit: size,
            attributes: {
                exclude: ['password'],//排除密码
                include: [
                    [Sequelize.col('depart_info.name'), 'dept_name'],
                    [Sequelize.col('role_info.name'), 'role_name'],
                ]
            },
            include: [
                {
                    model: Depart,
                    as: 'depart_info',//别名
                    // attributes:[['name','depart_name']], //depart:{name:''}
                    attributes: [], //depart:{name:''}
                },
                {
                    model: Role,
                    as: 'role_info',//别名
                    // attributes:[['name','role_name']], //depart:{name:''}
                    attributes: [], //depart:{name:''}
                }
            ]
        })
        return res || null
    }
}

export default new UseService